//
//  ZMFirstVC.m
//  demo
//
//  Created by chenzm on 2018/8/17.
//  Copyright © 2018年 chenzm. All rights reserved.
//

#import "ZMFirstVC.h"
#import "ZMPerson.h"

@interface ZMFirstVC ()
@property (nonatomic, strong) ZMPerson *person;

@end

@implementation ZMFirstVC

- (void)initView {
    [super initView];
    _person = [ZMPerson new];
    
}

- (void)buttonClick:(UIButton *)sender {
    
    // ------   设置属性值   -----
    _person.sex = @"1";
    unsigned int count = 0;
    // 动态获取类中的所有属性(包括私有)
    Ivar *ivar = class_copyIvarList(_person.class, &count);
    // 遍历属性找到对应字段
    for (int i = 0; i < count; i ++) {
        Ivar tempIvar = ivar[i];
        //ivar_getName:将IVar变量转化为字符串
        const char *varChar = ivar_getName(tempIvar);
        //ivar_getTypeEncoding：获取IVar的类型
        //const char *memberType = ivar_getTypeEncoding(tempIvar);
        NSString *varString = [NSString stringWithUTF8String:varChar];
        
        if ([varString isEqualToString:@"_name"]) {
            // 修改对应的字段值
            object_setIvar(_person, tempIvar, @"姓名");
        }
        
        NSLog(@"----->%@",varString);
        //私有类
        if ([varString isEqualToString:@"_age"]) {
            object_setIvar(_person, tempIvar, @"100");
        }
        
        //私有类
        if ([varString isEqualToString:@"address"]) {
            object_setIvar(_person, tempIvar, @"福建省厦门市");
        }
        //私有类
        if ([varString isEqualToString:@"mobile"]) {
            object_setIvar(_person, tempIvar, @"138****8888");
        }
        
    }
    
    // ----  利用KVC键值编码 获取属性值   -----
    NSString *str0 = [_person valueForKey:@"_name"];
    NSString *str1 = [_person valueForKey:@"_sex"];
    NSString *str2 = [_person valueForKey:@"_age"];
    NSString *str3 = [_person valueForKey:@"address"];
    NSString *str4 = [_person valueForKey:@"mobile"];
    NSMutableString *mstr = [NSMutableString string];
    [mstr appendString:str0];
    [mstr appendString:str1];
    [mstr appendString:str2];
    [mstr appendString:str3];
    [mstr appendString:str4];
    NSLog(@"非私有类：%@__%@\n私有类：%@__%@__%@__",str0,str1,str2,str3,str4);
    self.testLabelText = mstr ? mstr : @"更改属性值失败";
    
    
    // -----  设置属性值集成   ------
    [ZMTools zm_modifyClassIvar:_person keyArr:@[@"_name",@"address"] valueArr:@[@"rattan",@"我的家乡在福建"] isLog:YES];
    NSLog(@"%@___%@",[_person valueForKey:@"_name"],[_person valueForKey:@"address"]);
    
    
    // -----  键值编码设置属性值   ------
    [_person setValue:@"我的家乡还是在福建啊" forKey:@"address"];
    NSLog(@"=======%@", [_person valueForKey:@"address"]);
    
}

@end
